package com.tzx.socketdemo;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class UdpSocketActivity extends AppCompatActivity {
    EditText ipET;
    EditText portET;
    EditText textET;
    DatagramSocket datagramSocket;
    InetAddress inetAddress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tcp_activity_main);
        ipET = (EditText) findViewById(R.id.ip_tv);
        portET = (EditText) findViewById(R.id.port_tv);
        textET = (EditText) findViewById(R.id.message_tv);

        findViewById(R.id.btn_udp).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    if (ipET.getText().toString().length() > 0 &&
                            portET.getText().toString().length() > 0 &&
                            textET.getText().toString().length() > 0) {
                        final String ip = ipET.getText().toString();
                        final int port = Integer.parseInt(portET.getText().toString());
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                connectServerWithTCPSocketUDP(ip, port, textET.getText().toString());
                            }
                        }).start();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

    }

    protected void connectServerWithTCPSocketUDP(String ip, int port, String message) {
        try {
            if (datagramSocket == null || datagramSocket != null && datagramSocket.isConnected()) {
                datagramSocket = new DatagramSocket(port);
                inetAddress = InetAddress.getByName(ip);
            }
            String socketData = new String(message.getBytes(),"utf-8");
            DatagramPacket packet = new DatagramPacket(socketData.getBytes(), socketData.getBytes().length, inetAddress, port);
            datagramSocket.send(packet);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //主界面右上角的menu菜单
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Intent intent = null;
        switch (item.getItemId()) {
            case R.id.tcp:
                intent = new Intent(this, TcpSocketActivity.class);
                startActivity(intent);
                break;
        }
        return super.onOptionsItemSelected(item);
    }

}
